package cn.eoeit.mmy.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.eoeit.mmy.dao.IBaseDao;
import cn.eoeit.mmy.model.DeviceInfo;
import cn.eoeit.mmy.model.Factory;
import cn.eoeit.mmy.model.GongXuModel;
import cn.eoeit.mmy.model.Seller;
import cn.eoeit.mmy.service.ISystemService;
@Service("systemService")
public class SystemServiceImpl implements ISystemService {
	@Autowired
	private IBaseDao<DeviceInfo> deviceDao;
	@Autowired
	private IBaseDao<Factory> factoryDao;
	@Autowired
	private IBaseDao<Seller> sellerDao;
	
	
	@Autowired
	private IBaseDao<GongXuModel> gongxuDao;
	

	@Override
	public DeviceInfo finishAddDeviceInfo(DeviceInfo deviceInfo) {
		deviceInfo.setLastUpdateTime(System.currentTimeMillis());
		 deviceDao.save(deviceInfo);
		 return deviceInfo;
	}

	@Override
	public DeviceInfo findDeviceInfo(Integer uid) {
		return deviceDao.get(DeviceInfo.class, uid);
	}

	@Override
	public List<DeviceInfo> getDeviceInfos() {
		return deviceDao.find("from DeviceInfo ");
	}

	@Override
	public DeviceInfo finishDeleteDeviceInfo(Integer uid) {
		DeviceInfo device=deviceDao.get(DeviceInfo.class, uid);
		if(device!=null){
			deviceDao.delete(device);
		}
		return device;
	}

	@Override
	public DeviceInfo finishUpdateDeviceInfo(DeviceInfo deviceInfo) {
		
		DeviceInfo deviceInfo2=deviceDao.get(DeviceInfo.class, deviceInfo.getId());
		if(deviceInfo2!=null){
			deviceInfo2.setDescript(deviceInfo.getDescript()); 
			deviceInfo2.setName(deviceInfo.getName());
			deviceDao.update(deviceInfo2);
		}
		return deviceInfo2;
	}

	@Override
	public Factory finishAddFactory(Factory factory) {
		factory.setLastUpdateTime(System.currentTimeMillis());
		factoryDao.save(factory);
		 return factory;
	}

	@Override
	public Factory findFactory(Integer uid) {
		return factoryDao.get(Factory.class, uid);
	}

	@Override
	public List<Factory> getFactorys() {
		return factoryDao.find("from Factory ");
	}

	@Override
	public Factory finishDeleteFactory(Integer uid) {
		Factory device=factoryDao.get(Factory.class, uid);
		if(device!=null){
			factoryDao.delete(device);
		}
		return device;
	}

	@Override
	public Factory finishUpdateFactory(Factory f) {
		Factory factory=factoryDao.get(Factory.class, f.getId());
		if(factory!=null){
			factory.setAddress(f.getAddress()); 
			factory.setBianMa(f.getBianMa());
			factory.setLastUpdateTime(System.currentTimeMillis());
			factory.setName(f.getName());
			factory.setTel(f.getTel());
			factoryDao.update(factory);
		}
		return factory;
	}

	@Override
	public Seller finishAddSeller(Seller seller) {
		seller.setLastUpdateTime(System.currentTimeMillis());
		sellerDao.save(seller);
		 return seller;
	}

	@Override
	public Seller findSeller(Integer uid) {
		return sellerDao.get(Seller.class, uid);
	}

	@Override
	public List<Seller> getSellers() {
		return sellerDao.find("from Seller ");
	}

	@Override
	public Seller finishDeleteSeller(Integer uid) {
		Seller device=sellerDao.get(Seller.class, uid);
		if(device!=null){
			sellerDao.delete(device);
		}
		return device;
	}

	@Override
	public Seller finishUpdateSeller(Seller s) {
		Seller seller=sellerDao.get(Seller.class, s.getId());
		if(seller!=null){
			seller.setAddress(s.getAddress()); 
			seller.setBianMa(s.getBianMa());
			seller.setLastUpdateTime(System.currentTimeMillis());
			seller.setName(s.getName());
			seller.setTel(s.getTel());
			sellerDao.update(seller);
		}
		return seller;
	}

	@Override
	public GongXuModel finishAddfindGongXu(GongXuModel gongxu) {
		gongxu.setLastUpdateTime(System.currentTimeMillis());
		gongxuDao.save(gongxu);
		 return gongxu;
	}

	@Override
	public GongXuModel findGongXu(Integer uid) {
		return gongxuDao.get(GongXuModel.class, uid);
	}

	@Override
	public List<GongXuModel> getfindGongXus() {
		return gongxuDao.find("from GongXuModel ");
	}

	@Override
	public GongXuModel finishDeletefindGongXu(Integer uid) {
		GongXuModel device=gongxuDao.get(GongXuModel.class, uid);
		if(device!=null){
			gongxuDao.delete(device);
		}
		return device;
	}

	@Override
	public GongXuModel finishUpdateGongXu(GongXuModel s) {
		GongXuModel seller=gongxuDao.get(GongXuModel.class, s.getId());
		if(seller!=null){
			seller.setJiaJu(s.getJiaJu());
			seller.setLastUpdateTime(System.currentTimeMillis());
			seller.setName(s.getName());
			seller.setSheBeiId(s.getSheBeiId());
			gongxuDao.update(seller);
		}
		return seller;
	}


	@Override
	public DeviceInfo finishDeviceInfo(DeviceInfo user) {
		
		String hql="from UserInfo where userName=?";
		DeviceInfo  userinfo=deviceDao.get(hql, user.getName());
		if(userinfo!=null){	
			return null;
		}
		user.setLastUpdateTime(System.currentTimeMillis());
		
		Serializable result= deviceDao.save(user);
		 if(result!=null){
			 return user;
		 }
		 return null;
	}
}
